/**********************************************************************
 * Filename: welcome.js
 * 
 * The javascript functionality needed by the welcome/login page.
 * 
 * Author: Phillip Peterson
 * Date Created: 09/13/2009
 *
 **********************************************************************/


/**
 * @Desc Called on load of the body for this login page.  This function
 *       is basically a wrapper function which calls other functions
 *       which need to be called upon page load
 **/
function postLoad(){

	resetElements();

}


/**
 * @Desc Used to reset the login text fields and league select element
 *       upon unsuccessful login
 *
 **/
function resetElements(){

	document.getElementById("select_league").value = "NULL";
	document.getElementById("login_teamname").value = "";
	document.getElementById("login_password").value = "";

}

/**
* @Desc This function will get the league name, team name, and password
*       that the users enters to validate their credentials and bring
*       up the proper data for their team.  The function then uses an
*       HTTP Post Request to send this data to login.php, which the
*       following names.
**/
function checkTeamLogin() {

	//Get the login parameters which will be used to check against the database
	var leagueId = document.getElementById("select_league").value;
	var teamName = document.getElementById("login_teamname").value;
	var stayLogged = document.getElementById("login_remember").checked;			

	//Check any either field is null
	if(checkInput(teamName) == false){
		Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;    
    }  
	
	//Hash the password
	var password = getHash(document.getElementById("login_password"));
	
	
	//Send these login paramaters to the PHP function on the server using
	//an HTTP post request
	var paramNames = "type,leagueId,teamName,password,stayLogged";
	var paramVals  = "teamLogin," + leagueId + "," + teamName + "," + password + "," + stayLogged;
	var url        = "request_data.php";
	
	//Wait for a response from the login.php file
	var response = postRequest(paramNames, paramVals, url);

	if (response == 0){
		//Need to let the user know of the issue
		Popup.showModal('popupBadLogin',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});return false;
	}
	else if (response == "no_verify"){
	    Popup.showModal('no_verify',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});return false;
    }	    
	else if (response == 1){
		//Navigate to the team management page
		window.location = g_team_page;
	}
	
}
